package com.alinesno.cloud.base.storage.controller;

import java.io.File;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alinesno.cloud.base.storage.aop.ApiRecord;
import com.alinesno.cloud.base.storage.aop.ApiRecord.RecordType;
import com.alinesno.cloud.base.storage.entity.StorageFileEntity;
import com.alinesno.cloud.base.storage.enums.StrategyEnums;
import com.alinesno.cloud.base.storage.service.IStorageFileService;
import com.alinesno.cloud.base.storage.service.IStorageRequestsService;
import com.alinesno.cloud.base.storage.service.StorageService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.core.context.ApplicationContextProvider;
import com.alinesno.cloud.common.core.monitor.Server;

/**
 * <p> 前端控制器 </p>
 *
 * @author WeiXiaoJin
 * @since 2020-01-19 20:38:09
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("storageFile")
public class StorageController  {

	//日志记录
	private static final Logger log = LoggerFactory.getLogger(StorageController.class);

	@Autowired
	private IStorageFileService storageFileService; 
	
	@Autowired
	private IStorageRequestsService requestService ; 
	
	/**
	 * 基础面板  
	 * @param mmap
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/home")
	public String home(Model model) throws Exception {
		
		long fileCount = storageFileService.count() ; 
		long requestCount = requestService.count() ;
		
		model.addAttribute("fileCount", fileCount) ; 
		model.addAttribute("requestCount", requestCount) ; 
		
		Server server = new Server();
		server.copyTo();
		model.addAttribute("server", server);
	
		return "base/storage/home" ; 
	}
	
	/**
	 * 文件上传
	 * @param fileLoalAbcPath
	 * @param strategy
	 * @return
	 * @throws Exception 
	 */
	@ApiRecord(type = RecordType.API_UPLOAD)
	@ResponseBody
	@PostMapping("/upload")
	public String uploadFile(@RequestParam("file") MultipartFile file, 
			String suffix ,  
			String username,  
			String applicationName , 
			String strategy) throws Exception {
		
		log.debug("suffix:{} , username:{} , applicationName:{} , strategy:{}" , suffix , username , applicationName , strategy);
		
		strategy = StringUtils.isBlank(strategy)?StrategyEnums.QINIU.value():strategy ;
		
		String fileName = file.getOriginalFilename();
		String fileLocalAbcPath = "/tmp/"+UUID.randomUUID()+suffix ; 
		File tmpFile = new File(fileLocalAbcPath) ; 
		file.transferTo(tmpFile);
		
		StorageService storageService = (StorageService) ApplicationContextProvider.getBean(strategy+"StorageService") ; 
		FileUtils.forceDeleteOnExit(tmpFile);
		
		String path = storageService.uploadData(fileLocalAbcPath , fileName) ; 
		log.debug("cloud storage path :{}" , path);

		// 保存文件记录
		StorageFileEntity f = new StorageFileEntity() ; 
		f.setFileExt(suffix);
		f.setFileName(fileName);
		f.setFileSize(tmpFile.getTotalSpace());
		f.setFileUrl(path);
		f.setFilesource(strategy);
		f.setSaveType(suffix);
		f.setUrlDisk(tmpFile.getAbsolutePath());
		f.setDownloadNum(0);
		
		storageFileService.save(f) ; 
		
		return path ; 
	}

	
	@ApiRecord(type = RecordType.API_DOWNLOAD)
	@ResponseBody
	@PostMapping("/display")
	public String display() {
	
		return null ; 
	}

}


























